package com.wutuo.child.provider.service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.user.api.vo.UserInfoVo;
import cn.apestar.user.provider.info.dao.UserMapper;
import cn.apestar.user.provider.info.domain.User;
import cn.apestar.util.CommonUtil;
import com.wutuo.child.api.ChildAPIService;
import com.wutuo.child.api.request.ChildRequest;
import com.wutuo.child.api.request.ListChildRequest;
import com.wutuo.child.api.response.ChildResponse;
import com.wutuo.child.api.response.ParentChildResponse;
import com.wutuo.child.api.response.RollCallTaskResponse;
import com.wutuo.child.provider.dao.ChildMapper;
import com.wutuo.child.provider.domain.Child;
import com.wutuo.child.provider.enums.RollCallState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 孩子 <br>
 *
 * @author lvh
 * @version 1.0
 */
@Service public class ChildService extends BaseService<Child, Long> implements ChildAPIService {

    @Autowired private ChildMapper childMapper;

    @Autowired private UserMapper userMapper;

    @Override protected GenericMapper<Child, Long> getMapper() {

        // TODO Auto-generated method stub
        return childMapper;
    }

    public void addorUpdate(ChildRequest record) {

        if (record.getId() == null) {
            if (list(record).isEmpty())
                super.add(record);
        } else
            super.update(record);
    }

    /**
     * 删除
     *
     * @param userId
     * @param rosterId
     * @return void
     * @author lvh 2016年2月16日 上午11:18:28</br>
     */
    public void del(Long userId, Long rosterId) {

        ChildRequest model = new ChildRequest();
        model.setUserId(userId);
        model.setRosterId(rosterId);
        model.setDelFlag(false);
        childMapper.delChild(model);
    }

    public List<ChildResponse> lisForPage(ListChildRequest request) {

        request.setDelFlag(true);
        List<ChildResponse> list = childMapper.lisForPage(request);
        return list;
    }

    public List<Child> list(ChildRequest request) {

        request.setDelFlag(true);
        return childMapper.list(request);
    }

    public int updateLate(ChildRequest request) {

        // TODO Auto-generated method stub
        return childMapper.updateLate(request);
    }

    /**
     * 查询点名状态
     *
     * @param childId
     * @return
     */
    public List<RollCallTaskResponse> getChildDetail(String childId) {

        ListChildRequest record = new ListChildRequest();
        record.setChildListId(childId);
        record.setState(RollCallState.INTIME.getValue());
        List<RollCallTaskResponse> list = childMapper.getRollCallTaskResponse(record);
        getUserInfoListId(list, "teacherId", "userInfoVo");
        return list;
    }

    /**
     * 查询点名状态
     *
     * @param record
     * @return
     */
    public List<RollCallTaskResponse> listRollCallTaskResponsePage(ListChildRequest record) {

        record.setState(RollCallState.INTIME.getValue());
        List<RollCallTaskResponse> list = childMapper.getRollCallTaskResponse(record);
        getUserInfoListId(list, "teacherId", "userInfoVo");
        return list;
    }

    /**
     * 获取点名信息
     *
     * @param list
     * @param tKey
     * @param skey
     * @return void
     * @author lvh 2016年2月3日 下午1:24:45</br>
     */
    public void getUserInfoListId(List list, String tKey, String skey) {

        // TODO Auto-generated method stub
        StringBuffer idIn = CommonUtil.getLongInt(list, tKey);
        List<User> listChildResponse = userMapper.selectByUserAttr(new User());
        Map map = CommonUtil.listToObject(listChildResponse, "id");
        for (int i = 0; i < list.size(); i++) {
            Object modelS = list.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }
            String id = o.toString();
            if (map.containsKey(id)) {
                User users = (User) map.get(id);
                UserInfoVo userinfoVo = new UserInfoVo();
                CommonUtil.copyProperties(userinfoVo, users);
                userinfoVo.setUserName(users.getName());
                userinfoVo.setUserImg(users.getPhoto());
                CommonUtil.setProperty(modelS, skey, userinfoVo);
            }
        }
    }

    /**
     * 获取点名信息
     *
     * @param list
     * @param tKey
     * @param skey
     * @return void
     * @author lvh 2016年2月3日 下午1:24:45</br>
     */
    public void getTaskDetailByChildId(List list, String tKey, String skey) {

        // TODO Auto-generated method stub
        for (int i = 0; i < list.size(); i++) {
            Object modelS = list.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }
            String id = o.toString();
            List<RollCallTaskResponse> listChildResponse = getChildDetail("'" + id + "'");
            if (listChildResponse != null) {
                CommonUtil.setProperty(modelS, skey, listChildResponse);
            }
        }
    }

    /**
     * 根据孩子的ID返回家长的信息
     */
    public List<ParentChildResponse> getParentByChildId(String rosterIds) {

        return childMapper.getParentByChildId(rosterIds);
    }
}